<?php

class ArticleController extends Controller{
	public function actionIndex($id){
		$article = ArticleModel::model()->get_by_attrs(array(
			'site_id' 	=> SITE_ID,
			'_id' 		=> $id
		));
		if(isset($article['cate'])){
			$cate_id = $article['cate'];
			$cate = ArticleCateModel::model()->get_by_attrs(array(
				'site_id' 	=> SITE_ID,
				'_id' 		=> $cate_id
			));
			if(!empty($cate)){
				$this->assign('cate', $cate);
			}
		}
		if(!isset($article['desc'])){
			$article['desc'] = '';
		}
		$this->pageTitle = $article['title'] . ' - ' . Yii::app()->name;
		$this->render('index', array(
			'article' => $article
		));
	}
	
	public function actionCate($id){
		$cate = ArticleCateModel::model()->get_by_attrs(array(
			'site_id' 	=> SITE_ID,
			'_id' 		=> $id
		));
		
		$con = array(
			'site_id' 	=> SITE_ID,
			'cate' 		=> $id
		);
		$mod = ArticleModel::model();
		$total = $mod->count($con);
		$this->setPagination($total);
		
		$articles = $mod->find($con, null, $this->pager->offset, $this->pager->pageSize, array('rank'=>1));
		$this->render('cate', array(
			'cate' => $cate,
			'articles' => $articles
		));
	}
}